package com.psp.park.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.northend.util.AppTextUtil;
import com.northend.util.StringUtil;
import com.psp.park.controller.res.bean.RDemandBean;
import com.psp.park.controller.res.bean.RUserBean;
import com.psp.park.model.DemandBean;
import com.psp.park.model.ParkUserBean;
import com.psp.park.persist.dao.DemandDao;
import com.psp.park.service.DemandService;
import com.psp.park.service.exception.ServiceException;

@Service
public class DemandServiceImpl implements DemandService {
	
	@Autowired
	DemandDao demandImpl;

	@Override
	public PageResult<RDemandBean> getList(int page, int pageSize, String puid, int cateCode, String content, int handleState) {
		PageResult<RDemandBean> result = new PageResult<RDemandBean>();
		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}
		int count = demandImpl.selectCount(puid, cateCode, content);
		List<DemandBean> resList = demandImpl.selectList(page, pageSize, puid, cateCode, content);
		List<RDemandBean> resData = new ArrayList<>();
		for(DemandBean d : resList) {
			RDemandBean rc = new RDemandBean();
			rc.setName(d.getName());
			rc.setPhone(d.getPhone());
			rc.setDid(d.getDid());
			rc.setQuickCenter(DemandStrToArry(d.getQuickCenter()));
			rc.setIndustrialDesign(DemandStrToArry(d.getIndustrialDesign()));
			rc.setConceptualDesign(DemandStrToArry(d.getConceptualDesign()));
			rc.setStandardCertification(DemandStrToArry(d.getStandardCertification()));
			rc.setMidSmallBatch(DemandStrToArry(d.getMidSmallBatch()));
			rc.setTechServices(DemandStrToArry(d.getTechServices()));
			rc.setProductMarketing(DemandStrToArry(d.getProductMarketing()));
			rc.setCreateTime(d.getCreateTime().getTime());
			rc.setHandleState(d.getHandleState());
			
			ParkUserBean pu = d.getUser();
			RUserBean ruser = new RUserBean();
			ruser.setAccount(pu.getAccount());
			ruser.setUserName(pu.getNickName());
			ruser.setHeadIcon(pu.getHeadIcon());
			ruser.setPark(pu.getPark());
			rc.setUser(ruser);
			resData.add(rc);
		}
		result.setCount(count);
		result.setData(resData);
		return result;
	}
	
	private String[] DemandStrToArry(String demands) {
		if(StringUtil.isEmpty(demands)) {
			return null;
		}
		demands = demands.replaceAll("'", "");
 		demands = demands.substring(1,demands.length()-1);
		return demands.split(",");
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean EditInfo(String puid, String did, String name, String phone, String[] quickCenter,
			String[] industrialDesign, String[] conceptualDesign, String[] standardCertification,
			String[] midSmallBatch, String[] techServices, String[] productMarketing) {
		boolean flag = false;
		boolean isNew = StringUtil.isEmpty(did);

		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}
		DemandBean bean = new DemandBean();
		bean.setName(name);
		bean.setPhone(phone);
		bean.setQuickCenter(StringUtil.arrayToStr(quickCenter));
		bean.setIndustrialDesign(StringUtil.arrayToStr(industrialDesign));
		bean.setConceptualDesign(StringUtil.arrayToStr(conceptualDesign));
		bean.setStandardCertification(StringUtil.arrayToStr(standardCertification));
		bean.setMidSmallBatch(StringUtil.arrayToStr(midSmallBatch));
		bean.setTechServices(StringUtil.arrayToStr(techServices));
		bean.setProductMarketing(StringUtil.arrayToStr(productMarketing));
		bean.setUid(puid);
		if (isNew) {
			did = AppTextUtil.getPrimaryKey();
			bean.setDid(did);
			bean.setHandleState(0);//未处理
			flag = demandImpl.insert(bean) > 0;
		} else {
			bean.setDid(did);
			flag = demandImpl.update(bean) > 0;
		}

		if (!flag) {
			throw new ServiceException("fail");
		}
		return flag;
	}

	@Override
	public int getNum(String puid) {
		return demandImpl.selectCount(puid, 0, null);
	}

	@Override
	public boolean deleteById(String did) {
		List<String> dids = JSON.parseArray(did, String.class);
		return demandImpl.deleteById(dids) > 0;
	}


}
